package cxydmmszl.chapter02.t013;

import java.util.Scanner;

/**
 * <li style="color: red;">Prob</li>
 * 在单链表和双链表中删除倒数第 K 个节点
 * <li style="color: red;">Desc</li>
 * 分别实现两个函数，一个可以删除单链表中倒数第 K 个节点，另一个可以删除双链表中倒数第 K 个节点。
 * <li style="color: red;">Link</li> CD49
 *
 * @author habitplus
 * @since 2021-08-27 21:33
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        sc.close();

        Node head = genLinkedList(arr);

        Main test = new Main();
        head = test.removeLastKthNode02(head, k);

        StringBuilder sb = new StringBuilder();
        while (head != null) {
            sb.append(head.val).append(" ");
            head = head.next;
        }
        System.out.println(sb.toString());
    }

    /**
     * 删除单链表的倒数第 k 个节点（头结点法）
     */
    public Node removeLastKthNode01(Node head, int k) {
        if (head == null || k < 1) {
            return head;
        }
        Node fast = head;
        while (k > 0 && fast != null) {
            fast = fast.next;
            k--;
        }

        if (k > 0) {
            return head;
        }

        Node tou = new Node(0);
        tou.next = head;
        Node pre = tou;

        while (fast != null) {
            pre = head;
            head = head.next;
            fast = fast.next;
        }
        pre.next = head.next;
        return tou.next;

    }

    /**
     * 删除单链表的倒数第 k 个节点（法二）
     */
    public Node removeLastKthNode02(Node head, int k) {
        if (head == null || k < 1) {
            return head;
        }

        Node assist = head;
        while (assist != null) {
            assist = assist.next;
            k--;
        }

        if (k == 0) {
            // 删除第一个节点
            head = head.next;
        } else if (k < 0) {
            // 删除非第一个节点
            assist = head;
            // 实际指向的是要删除的节点的前一个节点
            while (++k != 0) {
                assist = assist.next;
            }
            // 删除 assist.next 指向的节点
            assist.next = assist.next.next;
        }
        return head;
    }

    /**
     * 删除双链表的倒数第 k 个节点
     */
    public DoubleNode removeLastKthNode(DoubleNode head, int k) {
        if (head == null || k < 1) {
            return head;
        }
        DoubleNode assist = head;
        while (assist != null) {
            assist = assist.next;
            k--;
        }

        if (k == 0) {
            // 删除的是第一个节点
            head = head.next;
            head.prev = null;
        } else if  (k < 0) {
            // 删除的是非第一个节点
            assist = head;
            while (++k != 0) {
                assist = assist.next;
            }

            DoubleNode newNext = assist.next.next;
            assist.next = newNext;
            if (newNext != null) {
                newNext.prev = assist;
            }
        }

        return head;

    }

    private static class Node {
        int val;
        Node next;

        public Node(int val) {
            this.val = val;
            this.next = null;
        }

    }

    private static class DoubleNode {
        int val;
        DoubleNode prev;
        DoubleNode next;

        public DoubleNode(int val) {
            this.val = val;
        }
    }

    /*
     * 尾插法
     */
    private static Node genLinkedList(int[] arr) {
        if (arr == null || arr.length == 0) {
            return null;
        }

        Node head = null, pre = null, node;
        for (int val : arr) {
            node = new Node(val);
            if (pre == null) {
                head = node;
                pre = head;
            } else {
                pre.next = node;
                pre = node;
            }
        }

        return head;
    }

    /*
     * 头插法
     */
    private static Node genLinkedList01(int[] arr) {
        if (arr == null || arr.length == 0) {
            return null;
        }

        Node next = null, node;
        for (int i = arr.length - 1; i >= 0; i--) {
            node = new Node(arr[i]);
            node.next = next;
            next = node;
        }

        return next;
    }
}
